package com.smartwebx.ha.biz.rainwater.real.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.base.Objects;
import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Ordering;
import com.google.common.collect.Range;
import com.google.common.primitives.Doubles;
import com.smartwebx.ha.biz.rainwater.real.WaterInfoManager;
import com.smartwebx.ha.dal.assign.bean.HaArea;
import com.smartwebx.ha.dal.realtime.dao.HjAndKjMapper;
import com.smartwebx.ha.dal.realtime.dao.RiverFloodControlMapper;
import com.smartwebx.ha.dal.realtime.dao.RsvrMapper;
import com.smartwebx.ha.dal.realtime.dao.RsvrfsrMapper;
import com.smartwebx.ha.dal.realtime.dao.WaterMapper;
import com.smartwebx.ha.dal.realtime.prod.ProductMapper;
import com.smartwebx.ha.dal.usebean.GisWater;
import com.smartwebx.ha.dal.usebean.HjAndKjBean;
import com.smartwebx.ha.dal.usebean.RiverFC;
import com.smartwebx.ha.dal.usebean.RsvrFlood;
import com.smartwebx.ha.dal.usebean.RsvrfsrBean;
import com.smartwebx.ha.util.GisStationInProvOrInCity;

@Service
public class WaterInfoManagerImpl implements WaterInfoManager {

	@Autowired
	private WaterMapper waterDao;
	@Autowired
	private ProductMapper productDao;
	@Autowired
	private RsvrfsrMapper rsvrfsrDao;
	@Autowired
	private RiverFloodControlMapper riverFloodControlDao;
	@Autowired
	private HjAndKjMapper hjAndKjDao;
	@Autowired
	private RsvrMapper rsvrDao;

	@SuppressWarnings("unchecked")
	@Override
	public List<GisWater> getLatestRiverGisInfo(Date queryDate, HaArea area) {
//		Calendar calendar = DateUtils.toCalendar(queryDate);
//		int month = calendar.get(Calendar.MONTH) + 1;
//		int day = calendar.get(Calendar.DAY_OF_MONTH);
//		int md = month * 100 + day;

		String areaCode = "";
		if (area.getGroupId() == 1) {
			areaCode = area.getCode().substring(0, 2);
		} else {
			areaCode = area.getCode().substring(0, 4);
		}
//		gisWaters = waterDao.getLatestWaterGisInfoStcdFilter(queryDate, md);
		
		Map<String,Object> params = Maps.newHashMap();
		params.put("tm", DateFormatUtils.format(queryDate, "yyyy-MM-dd HH:mm"));
		productDao.findGisPoints(params);
		List<GisWater> gisWaters = (List<GisWater>)params.get("result");
		
		
		for (GisWater gisWater : gisWaters) {
			if (gisWater.getDval() != null && gisWater.getVal() != null) {
				gisWater.setUpAndDownVal(gisWater.getVal() + "/"
						+ gisWater.getDval());
			} else if (gisWater.getDval() == null && gisWater.getVal() != null) {
				gisWater.setUpAndDownVal(gisWater.getVal().toString());
			} else if (gisWater.getVal() == null && gisWater.getDval() != null) {
				gisWater.setUpAndDownVal("/" + gisWater.getDval());
			} else {
				gisWater.setUpAndDownVal("");
			}

			if (gisWater.getInProv().startsWith(areaCode)) {
				gisWater.setInProvOrInCity(1);
			} else {
				gisWater.setInProvOrInCity(0);
			}
		}
		/*
		 * List<String> types = Lists.newArrayList("river", "reservoir", "weir",
		 * "pumping", "tide"); List<RiverFC> riverFCBeans =
		 * riverFloodControlDao.getFloodControlInfo(); List<RsvrfsrBean>
		 * rsvrfsrList = rsvrfsrDao.getAllRsvrfsr(); GisStationInProvOrInCity
		 * gisStationInProvOrInCity= new GisStationInProvOrInCity();
		 * 
		 * for (String type : types) { List<GisWater> tempGisWaters = null; if
		 * (Objects.equal("river", type)) { tempGisWaters = waterDao
		 * .getLatestRiverGisInfoStcdFilter(queryDate); } else if
		 * (Objects.equal("reservoir", type)) { tempGisWaters = waterDao
		 * .getLatestReservoirGisInfoStcdFilter(queryDate); } else if
		 * (Objects.equal("weir", type)) { tempGisWaters = waterDao
		 * .getLatestWeirGatesGisInfoStcdFilter(queryDate); } else if
		 * (Objects.equal("pumping", type)) { tempGisWaters = waterDao
		 * .getLatestPumpingStationGisInfoStcdFilter(queryDate); } else if
		 * (Objects.equal("tide", type)) { tempGisWaters = waterDao
		 * .getLatestTideGisInfoStcdFilter(queryDate); }
		 * 
		 * if (Objects.equal("reservoir", type)) { Date newDate =
		 * DateUtils.truncate(queryDate, Calendar.DATE); final Date date =
		 * DateUtils.setYears(newDate, 1970);
		 * 
		 * Iterable<RsvrfsrBean> nowRsvrfsrList = Iterables.filter( rsvrfsrList,
		 * new Predicate<RsvrfsrBean>() {
		 * 
		 * @Override public boolean apply(RsvrfsrBean input) { Range<Date> range
		 * = Range.closed( input.getBgmd(), input.getEdmd()); return
		 * range.contains(date); } });
		 * 
		 * for (GisWater gisWater : tempGisWaters) { gisWater.setWarValue(0d);
		 * for (RsvrfsrBean rsvrfsr : nowRsvrfsrList) { if (Objects
		 * .equal(rsvrfsr.getStcd(), gisWater.getStcd())) {
		 * gisWater.setWarValue(rsvrfsr.getFsltdz()); } }
		 * gisWater.setSttp(addRsvrfsrTypeToGisWater(gisWater));
		 * gisWater.setWarnLevel(addHjAndKjLevelToGisWater(gisWater));
		 * gisWater.setInProvOrInCity
		 * (gisStationInProvOrInCity.inProvOrInCity(gisWater, area)); } } else {
		 * for (GisWater gisWater : tempGisWaters) { gisWater.setWarValue(0d);
		 * for (RiverFC river : riverFCBeans) { if
		 * (Objects.equal(river.getStcd(), gisWater.getStcd())) {
		 * gisWater.setWarValue(river.getWarWater()); } }
		 * gisWater.setWarnLevel(addHjAndKjLevelToGisWater(gisWater));
		 * gisWater.setInProvOrInCity
		 * (gisStationInProvOrInCity.inProvOrInCity(gisWater, area)); } }
		 * gisWaters.addAll(tempGisWaters); }
		 * 
		 * ImmutableList<GisWater> filterGisWaters =
		 * FluentIterable.from(gisWaters).filter(new Predicate<GisWater>() {
		 * public boolean apply(GisWater input){ return input.getVal() != null;
		 * } }).toList();
		 * 
		 * 
		 * 
		 * Ordering<GisWater> gisWaterValOrdering = new Ordering<GisWater>(){
		 * public int compare(GisWater left,GisWater right){ Double leftVal =
		 * Objects.firstNonNull(left.getVal(),
		 * 0d)-Objects.firstNonNull(left.getWarValue(), 0d); Double rightVal =
		 * Objects.firstNonNull(right.getVal(),
		 * 0d)-Objects.firstNonNull(right.getWarValue(), 0d); return
		 * Doubles.compare(leftVal, rightVal); } }.reverse(); return
		 * gisWaterValOrdering.sortedCopy(filterGisWaters);
		 */
		return gisWaters;
	}

	@Override
	public String addHjAndKjLevelToGisWater(GisWater gisWater) {
		int hjLevel = 0;
		int kjLevel = 0;
		String hj = "";
		String kj = "";
		String warnLevel = "";
		List<HjAndKjBean> hjBeans = hjAndKjDao.selectHjByStcd(gisWater
				.getStcd());

		for (HjAndKjBean hjBean : hjBeans) {
			if (hjBean.getWz() != null) {
				if (gisWater.getVal().compareTo(hjBean.getWz()) > 0) {
					hjLevel++;
				}
			} else {
				break;
			}
		}
		List<HjAndKjBean> kjBeans = hjAndKjDao.selectKjByStcd(gisWater
				.getStcd());

		for (HjAndKjBean kjBean : kjBeans) {
			if (kjBean.getWz() != null) {
				if (gisWater.getVal().compareTo(
						kjBean.getWz() == null ? 0.0 : kjBean.getWz()) > 0) {
					kjLevel++;
				}
			} else {
				break;
			}
		}

		switch (hjLevel) {
		case 1:
			hj = "小洪水";
			break;
		case 2:
			hj = "中洪水";
			break;
		case 3:
			hj = "大洪水";
			break;
		case 4:
			hj = "特大洪水";
			break;
		default:
			hj = "";
			break;
		}

		switch (kjLevel) {

		case 1:
			kj = "轻度枯水";
			break;
		case 2:
			kj = "较重枯水";
			break;
		case 3:
			kj = "严重枯水";
			break;
		case 4:
			kj = "特别严重枯水";
			break;
		default:
			kj = "";
			break;
		}
		if (kj == "") {
			warnLevel = hj;
		} else if (hj == "") {
			warnLevel = kj;
		} else {
			warnLevel = hj + "," + kj;
		}
		return warnLevel;
	}

	@Override
	public String addRsvrfsrTypeToGisWater(GisWater gisWater) {
		RsvrFlood rsvrFlood = new RsvrFlood();
		rsvrFlood = rsvrDao.getRsvrFlood(gisWater.getStcd());
		// 设置水库类型
		gisWater.setRsvrtp(rsvrFlood == null || rsvrFlood.getRsvrtp() == null ? 9
				: rsvrFlood.getRsvrtp());
		String sttp = "";
		switch (gisWater.getRsvrtp()) {
		case 1:
			// 小二型水库
			sttp = "ST";
			break;
		case 2:
			// 小一型水库
			sttp = "SO";
			break;
		case 3:
			// 中型水库
			sttp = "MS";
			break;
		case 4:
			sttp = "BT";
			break;
		case 5:
			sttp = "BO";
			break;
		default:
			sttp = "OZ";
			break;
		}
		return sttp;
	}

}
